package com.rr.springcloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author RR
 * @date 2021/3/22 14:28
 */
@RestController
public class DiscoveryController {
    //从这里可以获取注册在eureka中的各个服务和具体服务的各个实例
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/payment/discovery")
    public Object getDiscoveryMessage() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            System.out.print(service+"\t");
        }
        System.out.println();

        List<ServiceInstance> instances = discoveryClient.getInstances("payment-service");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getServiceId() + "," + instance.getHost() + "," + instance.getPort());
        }
        return instances;
    }
}
